package com.googlecode.mvndroid;

import com.googlecode.mvndroid.tools.AidlTool;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.DirectoryScanner;

/**
 * @goal aidl
 * @description Generate java classes from .aidl files.
 *
 * <a href="mailto:1.024e3@gmail.com">Bartosz Duzy</a>
 */
public class Aidl_Mojo extends AndroidMojo {

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {

        ProcessBuilder processBuilder = new ProcessBuilder();
        AidlTool tool = new AidlTool(this);
        List<String> parameters = new ArrayList<String>();

        getLog().info("Compiling aidl files into Java classes...");

        parameters.add("-p" + getAndroidFramework().getAbsolutePath());
        parameters.add("-I" + getSrcDir().getAbsolutePath());

        String[] includes = {"**\\*.aidl"};
        DirectoryScanner ds = new DirectoryScanner();
        ds.setBasedir(getSrcDir());
        

        ds.setIncludes(includes);
        ds.scan();
        String[] files = ds.getIncludedFiles();

        getLog().debug("Found " + files.length + " .aidl file(s), generating java interfaces...");

        for (int i = 0; i < files.length; i++) {

            List<String> localParameters = new ArrayList<String>();
            localParameters.add(getSrcDir() + java.io.File.separator + files[i]);

            if (getAidlOutDir() != null) {
                localParameters.add(getAidlOutDir().getAbsolutePath());
            }
            
            parameters.addAll(localParameters);
            tool.setParameters(parameters);
            tool.execute(processBuilder);
        }

    }
}
